package com.tql.test_04;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/*
* CyclicBarrier 线程数量自增一
*
* */
public class CyclicBarrierTest {
    //举例，七个线程集齐七个龙珠，召唤神龙
    public static void main(String[] args) {
        //线程数达到指定个数，执行参数里面的线程
        CyclicBarrier cyclicBarrier = new CyclicBarrier(8,new Thread(()->{
            System.out.println("召唤神龙，许愿");
        },"悟空"));
        for (int i = 1; i <=7; i++) {
            final int temp=i;
            new Thread(()->{
                System.out.println("集齐第"+temp+"颗龙珠");
                try {
                    cyclicBarrier.await(); //加线程数+1
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            },String.valueOf(i)).start();
        }
    }
}
